package com.project.service.impl;

import com.project.bean.RoomBean;
import com.project.bean.StudentBean;

import com.project.mapper.IFacilityDao;
import com.project.mapper.IRoomDao;
import com.project.mapper.IStudentDao;
import com.project.service.IRoomService;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * 房间业务实现类
 */
public class RoomServiceImpl extends BaseService implements IRoomService {
    @Override
    public List<RoomBean> findByItem(String address, String roomState, String ifOk, String roomType) {
        SqlSession session = this.getSession();
        IRoomDao roomDao = session.getMapper(IRoomDao.class);
        List<RoomBean> list = roomDao.findByItem(address,roomState,ifOk,roomType);
        session.close();
       return list;
    }

    @Override
    public void addRoom(RoomBean room) {
        SqlSession session = this.getSession();
        IRoomDao roomDao = session.getMapper(IRoomDao.class);
        roomDao.addRoom(room);

        session.commit();
        session.close();
    }

    @Override
    public boolean delRoom(Integer id) {
        SqlSession session = this.getSession();
        IStudentDao studentDao = session.getMapper(IStudentDao.class);
        IRoomDao roomDao = session.getMapper(IRoomDao.class);
        IFacilityDao facilityDao = session.getMapper(IFacilityDao.class);
        if (studentDao.countByRoom(id)==0) {
            roomDao.delRoom(id);
            facilityDao.delete(id);
            session.commit();
            session.close();
            return true;
        }
        return false;
    }

    @Override
    //显示房间信息，同时，统计已住人数，
    //列出房间里的学生集合，列出学生集合，在学生对象中需要包含班级名称
    public RoomBean showRoomInfo(Integer id) {
//        RoomBean room = this.roomDao.showRoomInfo(id);
//        room.setStudentList(studentDao.findByRoom(id));
//        return room;


        SqlSession session = this.getSession();
        IRoomDao roomDao = session.getMapper(IRoomDao.class);
        RoomBean roomBean = roomDao.showRoomInfo(id);

        session.close();
        return roomBean;
    }

}
